package unclevenn.cloudmusic.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;
import unclevenn.cloudmusic.common.Response;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import unclevenn.cloudmusic.model.Music;
import unclevenn.cloudmusic.service.MusicService;
import unclevenn.cloudmusic.until.BeansToMap;
import unclevenn.cloudmusic.until.MinioUntil;

import java.util.Map;

@RestController
@RequestMapping("/music")
public class MusicController {
    @Autowired
    private MusicService musicService;

    @Autowired
    private MinioUntil minioUntil;

    @GetMapping("/index")
    public JSONObject index() {
        String project = "Hello World";
        return Response.success(project);
    }

    @GetMapping("list")
    public JSONObject list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit) {
        PageHelper.startPage(page, limit);
        PageInfo<Music> pageInfo = new PageInfo<>(musicService.getMusicList());
        return Response.success(pageInfo);
    }

    @GetMapping("get")
    public JSONObject get(Long id) throws Exception {
        Music music = musicService.getMusicById(id);
        if (null == music) {
            return Response.error("暂无数据");
        }
        Map<String, Object> map = BeansToMap.beanToMap(music);
        String playUrl = minioUntil.getPresignedObjectUrl(music.getType(), music.getOssName() + "." + music.getType(), 60);
        String singerImg = minioUntil.getPresignedObjectUrl("jpg", "jaychou.jpg", 60);
        String img = minioUntil.getPresignedObjectUrl("jpg", music.getOssName() + "." + "jpg", 60);
        map.put("mp3", playUrl);
        map.put("singerImg", singerImg);
        map.put("img", img);
        return Response.success(map);
    }
}
